Generic Class Instantiations

The declaration of class instantiations is the same as for any other class considering the rules for composing the name of the instantiation and the rules for replacing the formal generic types with the actual ones. This means, for each class instantiation G<T1, ..., Tn> occurring either explicitly in a typedef declaration or implicitly as a type name a C++ class declaration with the name associated to this class instantiation (T1_..._Tn_G) is declared. In this declaration, each occurrence of a type parameter Pi in the generic class declaration for G is replaced by the actual type Ti.

For example, consider the following SOS declaration of a generic class:

class Generic <X,Y>
{  
   X att;
   Y func (X);
};

An instantiation of that class:

typedef Generic<T1,T2> TG;

is translated into the following C++ type declaration:

class T1_T2_Generic : virtual public sos_Object
{  ...
   virtual T1    get_att();
   virtual void  set_att(T1);
   virtual T2    func (T1);
};
typedef T1_T2_Generic TG;

Note, that the naming conventions apply recursively, that means the type instantiation Generic<List<T1>,T2> is declared as T1_List_T2_Generic.

Example:

Let us illustrate the described SOS to C++ mapping using the generic class Set that is available as a predefined class in the SOS environment (see Section [*]):

schema agg
{
   ...

   class Collection<Entity> : sos_Aggregate {...};

   class Set<Entity> : Collection<Entity>
   {
   public:
      bool is_element (Entity);
      void insert (Entity);
      void operator+= (Set<Entity>);   // union
      ...
   };
}

For an example of the mapping to C++ classes have a look at the following application schema that is using the predefined class Set from schema agg.

with agg
schema Sample
{
   class sosFile {...};

   class sosDirectory
   {
      Set<sosFile>       files;
      Set<sosDirectory>  dirs;
   public:
      Set<sosFile>       ls ();
      void               rmdir();
      ...
   };
}

The SOS schema Sample will be translated into a C++ interface as follows:

class sosFile : virtual public sos_Object {...};

class sosFile_Set : virtual public sosFile_Collection
{
public:
   virtual bool is_element (sosFile);
   virtual void insert (sosFile);  
   virtual void operator+= (sosFile_Set);
   ...
};

class sosDirectory_Set : virtual public sosDirectory_Collection { ... };

class sosDirectory : virtual public sos_Object
{ 
   virtual sosFile_Set       get_files ();
   virtual void              set_files (sosFile_set);
   virtual sosDirectory_Set  get_dirs ();
   virtual void              set_dirs (sosDirectory_set);
public:
   virtual sosFile_Set       ls ();
   virtual void              rmdir;
   ...
};

$\Box$